﻿using DevExpress.XtraLayout.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using XuanFe.AppLaunch.Common;
using XuanFe.Database.BLL;
using XuanFe.Database.DAL;
using XuanFe.Database.Model;

namespace XuanFe.AppLaunch
{
    public partial class BookingForm : DevExpress.XtraEditors.XtraForm
    {
        private const string m_ModuleId = "0201";

        private int colIndexBOOKING_ID = 0;

        private List<StaffModel> m_Staffs = new List<StaffModel>();
        private bool m_AllowFollowStaffId;

        public BookingForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 清空记录
        /// </summary>
        private void ClearRecordToGrid()
        {
            this.gridView1.BeginUpdate();
            this.gridView1.Columns.Clear();

            DevExpress.XtraGrid.Columns.GridColumn c;
            int visibleIndex = 0;

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "CUSTOMER_NAME";
            c.Caption = "联系人";
            c.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
            new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Custom, "CUSTOMER_NAME", "总数")});
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "MOBILE_NUMBER_START";
            c.Caption = "手机号码";
            c.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
            new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Count)});
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "CLAIM_FLG";
            c.Caption = "需要送票标识";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "SALE_STAFF_NAME";
            c.Caption = "预订员工";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "SALE_TIME";
            c.Caption = "预订时间";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "BOOKING_TYPE_DESC";
            c.Caption = "预订渠道";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "BOOKING_STATUS_DESC";
            c.Caption = "订单状态";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexBOOKING_ID = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "BOOKING_ID";
            c.Caption = "订单号";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = false;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "TOTAL_AMOUNT";
            c.Caption = "报价";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "TOTAL_BASE";
            c.Caption = "底价";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "PAID_FLG";
            c.Caption = "已支付标识";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "TOTAL_PAID";
            c.Caption = "已收款";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "COLLECTION_STAFF_NAME";
            c.Caption = "收款员工";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "COLLECTION_TIME";
            c.Caption = "收款时间";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "PRINT_FLG";
            c.Caption = "已出票标识";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "PRINT_STAFF_NAME";
            c.Caption = "出票员工";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "PRINT_TIME";
            c.Caption = "出票时间";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.gridView1.EndUpdate();
        }

        /// <summary>
        /// 加载记录
        /// </summary>
        private void LoadRecordToGrid(QueryBookingModel model)
        {
            using (DataSet ds = BookingDAL.Instance.GetDataSet(model))
            {
                this.gridControl1.DataSource = ds.Tables[0];
                this.gridView1.BestFitColumns();
            }
        }
        private void BuildAll()
        {
            this.m_Staffs = StaffDAL.Instance.Gets(true);

            this.cmbStaff.Properties.Items.Clear();
            this.cmbStaff.Properties.Items.AddRange(this.m_Staffs);
        }
        /// <summary>
        /// 有效性校验
        /// </summary>
        /// <returns></returns>
        private bool ValidityCheck()
        {
            this.errorProvider1.ClearErrors();
            string message = string.Empty;
            if (this.chkQueryStaffId.Checked)
            {
                if (Global.Instance.MandatoryFieldEmpty(this.cmbStaff, false, ref message))
                {
                    this.errorProvider1.SetError(this.cmbStaff, message);
                    this.cmbStaff.Focus();
                    return (false);
                }
            }
            if (this.chkQueryRegisterTime.Checked)
            {
                if (this.dteStartTime.DateTime > this.dteEndTime.DateTime)
                {
                    this.errorProvider1.SetError(this.dteEndTime, "开始时间不能大于结束时间");
                    this.dteEndTime.Focus();
                    return (false);
                }
            }
            if (this.chkQueryName.Checked)
            {
                if (Global.Instance.MandatoryFieldEmpty(this.txtCustomer, false, ref message))
                {
                    this.errorProvider1.SetError(this.txtCustomer, message);
                    this.txtCustomer.Focus();
                    return (false);
                }
            }
            if (this.chkQueryMobileNumber.Checked)
            {
                if (Global.Instance.MandatoryFieldEmpty(this.txtMobileNumber, false, ref message))
                {
                    this.errorProvider1.SetError(this.txtMobileNumber, message);
                    this.txtMobileNumber.Focus();
                    return (false);
                }
            }
            if (!this.chkQueryStaffId.Checked &&
                !this.chkQueryRegisterTime.Checked &&
                !this.chkQueryName.Checked &&
                !this.chkQueryMobileNumber.Checked)
            {
                Global.Instance.InfoDlg("请选择查询条件，至少得有一项");
                return (false);
            }
            return (true);
        }
        private void ShowCustomerDetailForm(string customerId)
        {
            MainForm main = this.ParentForm as MainForm;
            if (main == null)
            {
                return;
            }

            using (DevExpress.Utils.WaitDialogForm waitdlg = new DevExpress.Utils.WaitDialogForm("正在加载...", "请稍候！"))
            {
                string key = string.Concat("C", customerId);
                CustomerDetailForm dlg = new CustomerDetailForm(customerId) { Tag = key };
                Global.Instance.ShowMdiForm<CustomerDetailForm>(main, dlg, key);
            }
        }
        private void BookingForm_Load(object sender, EventArgs e)
        {
            try
            {
                DateTime now = DateTime.Now;
                this.dteStartTime.DateTime = now.Date;
                this.dteEndTime.DateTime = now.Date.AddDays(1).AddSeconds(-1);

                this.chkQueryStaffId_CheckedChanged(null, null);
                this.chkQueryRegisterTime_CheckedChanged(null, null);
                this.chkQueryName_CheckedChanged(null, null);
                this.chkQueryMobileNumber_CheckedChanged(null, null);

                //this.chkQueryMobileNumber.Checked = true;
                this.chkFuzzyMobileNumber.Checked = true;
                this.chkFuzzyName.Checked = true;

                this.BuildAll();
                this.ClearRecordToGrid();
                this.LoadRecordToGrid(new QueryBookingModel() { QuerySaleTime = true, StartSaleTime = now.Date, EndSaleTime = now.Date.AddDays(1).AddSeconds(-1) });

                StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_ModuleId, "浏览记录");

                // 添加客户
                if (!LoginInfo.Instance.CheckPermission("020102"))
                {
                    this.lcAppend.Visibility = LayoutVisibility.Never;
                }
                this.m_AllowFollowStaffId = LoginInfo.Instance.CheckPermission("020107");
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.ValidityCheck()) return;

                Global.Instance.BeginWait();

                string staffId = string.Empty;
                if (this.cmbStaff.SelectedIndex != -1 && this.cmbStaff.SelectedItem != null)
                {
                    staffId = (this.cmbStaff.SelectedItem as StaffModel).Id;
                }
                QueryBookingModel model = new QueryBookingModel()
                {
                    QuerySaleStaffId = this.chkQueryStaffId.Checked,
                    SaleStaffId = staffId,
                    QuerySaleTime = this.chkQueryRegisterTime.Checked,
                    StartSaleTime = this.dteStartTime.DateTime,
                    EndSaleTime = this.dteEndTime.DateTime,
                    QueryName = this.chkQueryName.Checked,
                    Name = this.txtCustomer.Text.Trim(),
                    FuzzyName = this.chkFuzzyName.Checked,
                    QueryMobileNumber = this.chkQueryMobileNumber.Checked,
                    MobileNumber = this.txtMobileNumber.Text.Trim(),
                    FuzzyMobileNumber = this.chkFuzzyMobileNumber.Checked
                };
                this.LoadRecordToGrid(model);
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
            finally
            {
                Global.Instance.EndWait();
            }
        }

        private void chkQueryStaffId_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                this.cmbStaff.Enabled = this.chkQueryStaffId.Checked;
                if (this.chkQueryStaffId.Checked)
                {
                    this.cmbStaff.Focus();
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void chkQueryRegisterTime_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                this.dteStartTime.Enabled = this.dteEndTime.Enabled = this.chkQueryRegisterTime.Checked;
                if (this.chkQueryRegisterTime.Checked)
                {
                    this.dteStartTime.Focus();
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void chkQueryName_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                this.txtCustomer.Enabled = this.chkFuzzyName.Enabled = this.chkQueryName.Checked;
                if (this.chkQueryName.Checked)
                {
                    this.txtCustomer.Focus();
                    this.txtCustomer.SelectAll();
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void chkQueryMobileNumber_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                this.txtMobileNumber.Enabled = this.chkFuzzyMobileNumber.Enabled = this.chkQueryMobileNumber.Checked;
                if (this.chkQueryMobileNumber.Checked)
                {
                    this.txtMobileNumber.Focus();
                    this.txtMobileNumber.SelectAll();
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnAppend_Click(object sender, EventArgs e)
        {
            try
            {
                MainForm main = this.ParentForm as MainForm;
                if (main == null)
                {
                    return;
                }

                using (DevExpress.Utils.WaitDialogForm waitdlg = new DevExpress.Utils.WaitDialogForm("正在加载...", "请稍候！"))
                {
                    EditBookingForm dlg = new EditBookingForm(null, null);
                    Global.Instance.ShowMdiForm<EditBookingForm>(main, dlg);
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int r = this.gridView1.FocusedRowHandle;
                if (r < 0) return;

                MainForm main = this.ParentForm as MainForm;
                if (main == null)
                {
                    return;
                }

                using (DevExpress.Utils.WaitDialogForm waitdlg = new DevExpress.Utils.WaitDialogForm("正在加载...", "请稍候！"))
                {
                    string bookingId = Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexBOOKING_ID]));
                    string key = string.Concat("B", bookingId);

                    BookingDetailForm dlg = new BookingDetailForm(bookingId) { Tag = key };
                    Global.Instance.ShowMdiForm<BookingDetailForm>(main, dlg, key);
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }
    }
}